package com.aw.guiguzi.module.common.presenters

import com.aw.baselib.base.AwCommonPresenter
import com.aw.baselib.net.callback.AwApiCallback
import com.aw.baselib.util.AwDataUtil
import com.aw.baselib.util.AwLog
import com.aw.baselib.util.AwSystemUtil
import com.aw.guiguzi.bean.AllTokenResultBean
import com.aw.guiguzi.bean.LoginResultBean
import com.aw.guiguzi.bean.UserInfoBean
import com.aw.guiguzi.module.common.views.LoginView
import com.aw.guiguzi.module.common.views.LoginView.Presenter
import com.aw.guiguzi.net.observers.NetObCommon
import com.aw.guiguzi.util.AppUserUtil.saveToken
import com.aw.guiguzi.util.AppUserUtil.saveUser
import com.aw.guiguzi.util.AppUserUtil.token

/**
 * @author : hzw
 */
class LoginPresent(private val mView: LoginView.View) :
    AwCommonPresenter(), Presenter {
    override fun login(account: String?, pwd: String?) {
        addIOSubscription(
            NetObCommon.login(account, pwd),
            object : AwApiCallback<LoginResultBean>(mView, true, true) {
                override fun onSuccess(result: LoginResultBean?) {
                    AwLog.d("login onSuccess: $result")
                    saveToken(result?.accessToken)
                    getUserInfo()
                }

                override fun onFail(code: Int, msg: String) {
                    super.onFail(code, msg)
                    mView.showMsg(msg)
                    AwLog.d("login onFail code: $code ,msg: $msg")
                }
            })
    }

    override fun getUserInfo() {
        addIOSubscription(
            NetObCommon.getUserInfo(),
            object : AwApiCallback<UserInfoBean>(mView, true, true) {
                override fun onSuccess(result: UserInfoBean) {
                    AwLog.d("login getUserInfo: $result")
                    result.email = AwSystemUtil.getSystemModel() + "@" + result.id + ".com"
                    saveUser(result)
                    updateUserInfo(result)
                }

                override fun onFail(code: Int, msg: String) {
                    super.onFail(code, msg)
                    mView.showMsg(msg)
                    AwLog.d("login getUserInfo onFail code: $code ,msg: $msg")
                }
            })
    }

    override fun updateUserInfo(bean: UserInfoBean?) {
        addIOSubscription(
            NetObCommon.updateUserInfo(bean),
            object : AwApiCallback<Boolean>(mView, true, true) {
                override fun onSuccess(result: Boolean) {
                    AwLog.d("login updateUserInfo: $result")
                    getAllDeviceToken()
                    //                if(result) {
//                    mView.loginHandleSuccess();
//                } else {
//                    mView.showMsg("Login Fail");
//                }
                }

                override fun onFail(code: Int, msg: String) {
                    super.onFail(code, msg)
                    mView.showMsg(msg)
                    AwLog.d("login updateUserInfo onFail code: $code ,msg: $msg")
                }
            })
    }

    override fun getAllDeviceToken() {
        addIOSubscription(
            NetObCommon.getAllDeviceToken(),
            object : AwApiCallback<AllTokenResultBean>(mView, true, true) {
                override fun onSuccess(result: AllTokenResultBean) {
                    AwLog.d("login getAllDeviceToken: $result")
                    if (!AwDataUtil.isEmpty(result.list) && result.list!!.size > 1) {
                        for (itemBean in result.list!!) {
                            if (itemBean.accessToken != token) {
                                delToken(itemBean.accessToken)
                            }
                        }
                    }
                    mView.loginHandleSuccess()
                }

                override fun onFail(code: Int, msg: String) {
                    super.onFail(code, msg)
                    mView.showMsg(msg)
                    AwLog.d("login updateUserInfo onFail code: $code ,msg: $msg")
                }
            })
    }

    override fun delToken(accessToken: String?) {
        addIOSubscription(
            NetObCommon.delToken(accessToken),
            object : AwApiCallback<Boolean>(mView, true, true) {
                override fun onSuccess(result: Boolean) {
                    AwLog.d("login delToken: $result")
                }

                override fun onFail(code: Int, msg: String) {
                    super.onFail(code, msg)
                    AwLog.d("login delToken onFail code: $code ,msg: $msg")
                }
            })
    }
}